package cloud.maque.module.system.service.impl;

import cloud.maque.common.core.exceptions.BusinessException;
import cloud.maque.common.core.request.PageRequest;
import cloud.maque.common.core.utils.TokenUserUtil;
import cloud.maque.common.utils.PageRequestUtil;
import cloud.maque.module.system.entity.SysConfig;
import cloud.maque.module.system.entity.SysDept;
import cloud.maque.module.system.mapper.DeptMapper;
import cloud.maque.module.system.service.DeptService;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.Date;
import java.util.List;

@Service
public class DeptServiceImpl extends ServiceImpl<DeptMapper, SysDept> implements DeptService {


    @Autowired
    TokenUserUtil userUtil;
    @Autowired
    PageRequestUtil util;

    @Override
    public IPage<SysDept> GetList(PageRequest<SysDept> pageReq) {
        Page<SysDept> page = util.GetPage(SysConfig.class, pageReq);
        return this.page(page, getQuery(pageReq.getData()));
    }

    @Override
    public List<SysDept> GetList(SysDept data) {
        return this.list(getQuery(data));
    }

    /**
     * 取得条件查询
     *
     * @param data
     * @return
     */

    private QueryWrapper<SysDept> getQuery(SysDept data) {
        QueryWrapper<SysDept> query = new QueryWrapper<>();
        if (!StrUtil.isEmptyIfStr(data.getDeptName())) {
            query.lambda().like(SysDept::getDeptName, data.getDeptName());
        }
        if (!StrUtil.isEmptyIfStr(data.getDeptCode())) {
            query.lambda().like(SysDept::getDeptCode, data.getDeptCode());
        }

        query.lambda().orderByAsc(SysDept::getOrderNum);

        return query;

    }

    @Override
    public SysDept SaveDept(SysDept data) {
        Assert.hasText(data.getDeptName(), "部门名称不能为空");

        if (StrUtil.isEmptyIfStr(data.getDeptId())) {
            data.setCreateBy(userUtil.GetUserName());
            data.setCreateTime(new Date());
            data.setUpdateBy(data.getCreateBy());
            data.setUpdateTime(new Date());
        } else {
            SysDept oldPost = this.baseMapper.selectById(data.getDeptId());
            if (oldPost != null) {
                data.setCreateTime(oldPost.getCreateTime());
                data.setCreateBy(oldPost.getCreateBy());
            }
            data.setUpdateBy(userUtil.GetUserName());
            data.setUpdateTime(new Date());

        }

        SysDept oldPost = null;
        if (!StrUtil.isEmptyIfStr(data.getDeptCode())) {
            oldPost = GetByDeptCode(data.getDeptCode());
            if (oldPost != null && !oldPost.getDeptId().equals(data.getDeptId())) {
                if (oldPost.getDeptCode().equalsIgnoreCase(data.getDeptCode())) {
                    throw new BusinessException("部门代码已存在");
                }
            }
        }

        oldPost = GetByDeptName(data.getDeptName(), data.getParentId());
        if (oldPost != null && !oldPost.getDeptId().equals(data.getDeptId())) {
            throw new BusinessException("同级部门下已存在 " + data.getDeptName());
        }

        this.saveOrUpdate(data);
        return data;
    }

    private SysDept GetByDeptCode(String deptCode) {

        QueryWrapper<SysDept> query = new QueryWrapper<>();
        query.lambda().eq(SysDept::getDeptCode, deptCode);
        return this.getOne(query, false);
    }


    private SysDept GetByDeptName(String deptName, String parentId) {

        QueryWrapper<SysDept> query = new QueryWrapper<>();
        query.lambda().eq(SysDept::getDeptName, deptName);
        query.lambda().eq(SysDept::getParentId, parentId);
        return this.getOne(query, false);
    }

    /**
     * 修改状态
     *
     * @param data
     * @return
     */
    @Override
    public SysDept EditStatus(SysDept data) {
        Assert.hasText(data.getDeptId(), "ID不能为空");

        SysDept post = this.getById(data.getDeptId());
        if (post == null) {
            throw new BusinessException("部门不存在");
        }
        if ("1".equals(post.getStatus())) {
            post.setStatus("0");
        } else {
            post.setStatus("1");
        }
        UpdateWrapper<SysDept> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda().eq(SysDept::getDeptId, post.getDeptId());
        updateWrapper.lambda().set(SysDept::getStatus, post.getStatus());
        updateWrapper.lambda().set(SysDept::getUpdateBy, userUtil.GetUserName());
        updateWrapper.lambda().set(SysDept::getUpdateTime, new Date());
        this.update(updateWrapper);
        return post;
    }

    /**
     * 逻辑删除
     *
     * @param deptId
     * @return
     */
    @Override
    public Boolean DeleteDept(String deptId) {
        UpdateWrapper<SysDept> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda().eq(SysDept::getDeptId, deptId).set(SysDept::getDelFlag, "1");
        return this.update(updateWrapper);
    }
}
